﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.IO;
using DevExpress.XtraBars.Helpers;

namespace ADB_QuanLyThuVien
{
    public partial class frmTrangChu : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private Account acc;

        public frmTrangChu()
        {
            InitializeComponent();
        }

        public frmTrangChu(Account acc)
        {
            this.acc = acc;
            InitializeComponent();
        }
        public bool HaveOpenTab(string tabTittle)
        {
            for (int i = 0; i < xtraTabControl1.TabPages.Count; i++)
            {
                if (xtraTabControl1.TabPages[i].Text == tabTittle)
                {
                    xtraTabControl1.SelectedTabPageIndex = i;
                    return true;
                }
            }
            return false;
        }
        //Dong XtraTabControl
        private void xtraTabControl1_CloseButtonClick(object sender, EventArgs e)
        {
            DevExpress.XtraTab.XtraTabPage tab = xtraTabControl1.SelectedTabPage;
            xtraTabControl1.TabPages.Remove(tab);
            int a = tab.TabIndex;
            if (a > 1)
            {
                xtraTabControl1.SelectedTabPageIndex = a - 1;
            }
        }
       

        private void frmTrangChu_Load(object sender, EventArgs e)
        {
            //Chọn skin cho phần mềm
            string fileName;
            fileName = Application.StartupPath.ToString() + @"\Skin.txt";
            if (System.IO.File.Exists(fileName) == false)
            {
                defaultLookAndFeel1.LookAndFeel.SetSkinStyle("Money Twins"); // Tạo skin ban đầu cho ứng dụng

            }
            //chọn skin
            else
            {
                StreamReader objRead = new StreamReader(fileName, false);
                defaultLookAndFeel1.LookAndFeel.SetSkinStyle(objRead.ReadLine().ToString());
                objRead.Close();
            }
            //chọn skin trong phần ribbon Gal
            SkinHelper.GetSkinCaptionBySkinName("alo");
            SkinHelper.InitSkinGallery(ribbonGalleryBarItem1);
            if (acc == null)
            {
                rb_DocGia.Visible = false;
                rb_NhanVien.Visible = false;
                btb_dangxuat.Enabled = false;
            }
            else
            {
                if (DAO_Member.isEmployee(acc))
                    rb_DocGia.Visible = false;
                else
                    rb_NhanVien.Visible = false;
                barButtonItem8.Enabled = false;
            }
        }

        private void btn_nhomtacgia_ItemClick(object sender, ItemClickEventArgs e)
        {
            var frm = new About();
            frm.ShowDialog();
        }

        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            var frm = new frmLogin();
            frm.Show();
            this.Hide();
        }

        private void btnTraCuuSach_ItemClick(object sender, ItemClickEventArgs e)
        {
            string tabTittle = "Tra cứu sách"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            if (!HaveOpenTab(tabTittle)) //Neu tab nay chua ton tai, add tab nay vao TabControl
            {
                xtraTabControl1.TabPages.Add(tab);
                var hien = new frmTraCuuSach(acc,this);
                hien.TopLevel = false;
                hien.Dock = DockStyle.Fill;
                tab.Controls.Add(hien);
                hien.Show();
                xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.TabPages.Count - 1;
            }
        }

        private void btnTTDangKy_ItemClick(object sender, ItemClickEventArgs e)
        {
            string tabTittle = "Sách mượn"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            if (!HaveOpenTab(tabTittle)) //Neu tab nay chua ton tai, add tab nay vao TabControl
            {
                xtraTabControl1.TabPages.Add(tab);
                var hien = new frmBorrowBook(acc);
                hien.TopLevel = false;
                hien.Dock = DockStyle.Fill;
                tab.Controls.Add(hien);
                hien.Show();
                xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.TabPages.Count - 1;
            }
        }      
        private void btn_muonsach_ItemClick(object sender, ItemClickEventArgs e)
        {
            string tabTittle = "Mượn sách"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            if (!HaveOpenTab(tabTittle)) //Neu tab nay chua ton tai, add tab nay vao TabControl
            {
                xtraTabControl1.TabPages.Add(tab);
                var hien = new frmPhieuMuonSach(acc,this);
                hien.TopLevel = false;
                hien.Dock = DockStyle.Fill;
                tab.Controls.Add(hien);
                hien.Show();
                xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.TabPages.Count - 1;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lb_dongho.Text = DateTime.Now.Hour.ToString("00") + " : " + DateTime.Now.Minute.ToString("00") + " : " + DateTime.Now.Second.ToString("00");
        }

        private void btn_dkonline_ItemClick(object sender, ItemClickEventArgs e)
        {
            //var frm = new frmDangKyMuonSach(acc);
            //frm.Show();
            //this.Hide();
            string tabTittle = "Đăng ký mượn"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            if (!HaveOpenTab(tabTittle)) //Neu tab nay chua ton tai, add tab nay vao TabControl
            {
                xtraTabControl1.TabPages.Add(tab);
                var hien = new frmDangKyMuonSach(acc,this);
                hien.TopLevel = false;
                hien.Dock = DockStyle.Fill;
                tab.Controls.Add(hien);
                hien.Show();
                xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.TabPages.Count - 1;
            }
        }

        private void btn_thoat_ItemClick(object sender, ItemClickEventArgs e)
        {
            Application.Exit();
        }

        private void frmTrangChu_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void barButtonItem8_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            var frm = new frmLogin();
            frm.Show();
            this.Hide();
        }

        private void btb_dangxuat_ItemClick(object sender, ItemClickEventArgs e)
        {
            var frm = new frmLogin();
            frm.Show();
            this.Hide();
        }

        private void btnTraSach_ItemClick(object sender, ItemClickEventArgs e)
        {
            string tabTittle = "Trả sách"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            if (!HaveOpenTab(tabTittle)) //Neu tab nay chua ton tai, add tab nay vao TabControl
            {
                xtraTabControl1.TabPages.Add(tab);
                var hien = new frmReturnBook(acc);
                hien.TopLevel = false;
                hien.Dock = DockStyle.Fill;
                tab.Controls.Add(hien);
                hien.Show();
                xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.TabPages.Count - 1;
            }
        }

        private void btnDanhSachMuon_ItemClick(object sender, ItemClickEventArgs e)
        {
            string tabTittle = "DS sách mượn"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            if (!HaveOpenTab(tabTittle)) //Neu tab nay chua ton tai, add tab nay vao TabControl
            {
                xtraTabControl1.TabPages.Add(tab);
                var hien = new frmDanhSachMuonSach(acc, this);
                hien.TopLevel = false;
                hien.Dock = DockStyle.Fill;
                tab.Controls.Add(hien);
                hien.Show();
                xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.TabPages.Count - 1;
            }
        }

        private void btnDanhSachTra_ItemClick(object sender, ItemClickEventArgs e)
        {
            string tabTittle = "DS sách trả"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            if (!HaveOpenTab(tabTittle)) //Neu tab nay chua ton tai, add tab nay vao TabControl
            {
                xtraTabControl1.TabPages.Add(tab);
                var hien = new frmDanhSachTraSach(acc, this);
                hien.TopLevel = false;
                hien.Dock = DockStyle.Fill;
                tab.Controls.Add(hien);
                hien.Show();
                xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.TabPages.Count - 1;
            }
        }  
        

      

       

    }
}